package com.mycat.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * NIO 客户端。
 * 同步非阻塞IO。
 * 数据面向-缓冲区。
 * @author roger.
 * @email 190642964@qq.com
 * @create 2017-02-17
 */
public class NIOClient {

    SocketChannel client;
    Selector selector;
    InetSocketAddress serverAddress = new InetSocketAddress("localhost", 8888);
    ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);
    ByteBuffer sendBuffer = ByteBuffer.allocate(1024);

    public NIOClient() throws IOException {
        // 先开路
        client = SocketChannel.open();
        client.configureBlocking(false);

        client.connect(serverAddress);

        selector = Selector.open();

        client.register(selector, SelectionKey.OP_CONNECT);
    }

    public void session() throws IOException {
        // 先判断是否已经建立连接
        if (client.isConnectionPending()) {
            client.finishConnect();
            System.out.println("请在控制台登记姓名：");
            client.register(selector, SelectionKey.OP_WRITE);
        }

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String name = scanner.nextLine();
            if ("".equals(name)) {
                continue;
            }
            process(name);
        }
    }

    public void process(String name) throws IOException {
        boolean unFinish = true;
        while (unFinish) {
            // 判断当前有没有客户来注册，有没有排队的，有没有取号的
            int i = selector.select();
            if (i == 0) {
                continue;
            }

            Set<SelectionKey> keys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = keys.iterator();

            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();

                if (key.isWritable()) {

                    sendBuffer.clear();
                    sendBuffer.put(name.getBytes());
                    sendBuffer.flip();

                    client.write(sendBuffer);

                    client.register(selector,SelectionKey.OP_READ);

                } else if (key.isReadable()) {

                    receiveBuffer.clear();
                    int len = client.read(receiveBuffer);
                    if(len > 0){
                        receiveBuffer.flip();
                        System.out.println("获取到服务端反馈的消息："+new String(receiveBuffer.array(),0,len));
                        client.register(selector,SelectionKey.OP_WRITE);
                        unFinish = false;
                    }


                }
            }
        }
    }


    public static void main(String[] args) throws IOException {
        new NIOClient().session();
    }
}
